package demo1.web.servlet.userMaiapi;

import demo1.dao.Mapper.userMaiMapper;
import demo1.pojo.userMai;
import demo1.dao.sql.userMaisql;
import demo1.service.userMaiService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet("/userMaiLogin")
public class userMaiLogin extends HttpServlet {//商户登录

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        resp.setContentType("text/html;charset = utf-8");
        PrintWriter writer = resp.getWriter();
        //判断用户是否登录成功
        userMaiService check = new userMaiService();
        try {
            int num = check.checkLogin(username,password);
            if (num == 1) {//如果存在这个用户
                userMai users = check.write(username, password);//写入用户
                writer.write("登录成功");

                HttpSession session = req.getSession();
                session.setAttribute("users", users);
                resp.sendRedirect("/logindemo2/showUserMai.jsp");

            } else {//如果没有这个用户
                writer.write("登录失败");
                //登录失败
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.doGet(req,resp);
    }
}
